#ifndef _KEY_DRV_H_
#define _KEY_DRV_H_

#include "main.h"

typedef struct
{
    uint32_t rcc;
    GPIO_TypeDef *gpio;
    uint16_t pin;
} Key_GPIO_t;

static Key_GPIO_t g_gpioList[] =
{
    {RCC_AHB_PERIPH_GPIOB, CW_GPIOB, GPIO_PIN_2}, // key1
    {RCC_AHB_PERIPH_GPIOB, CW_GPIOB, GPIO_PIN_3}, // key2
    {RCC_AHB_PERIPH_GPIOB, CW_GPIOB, GPIO_PIN_4}, // key3
    {RCC_AHB_PERIPH_GPIOB, CW_GPIOB, GPIO_PIN_5}, // key4
};

#define KEY_NUM_MAX (sizeof(g_gpioList) / sizeof(g_gpioList[0]))

typedef enum
{
    KEY_RELEASE = 0, // 释放松开
    KEY_CONFIRM,     // 消抖确认
    KEY_SHORTPRESS,  // 短按
    KEY_LONGPRESS,   // 长按
} KEY_STATE;

#define CONFIRM_TIME 10                // 按键消抖时间窗10ms
#define DOUBLE_CLICK_INTERVAL_TIME 300 // 两次抬起时间窗300ms，用来判断是否双击
#define LONGPRESS_TIME 1000            // 长按时间窗1000ms

typedef struct
{
    KEY_STATE keyState;
    uint8_t singleClickNum;
    uint64_t firstIoChangeSysTime;
    uint64_t firstReleaseSysTime;
} Key_Info_t;

#define KEY1_SHORT_PRESS 0x01  // KEY1短按码值 0x01
#define KEY2_SHORT_PRESS 0x02  // KEY2短按码值 0x02
#define KEY3_SHORT_PRESS 0x03  // KEY3短按码值 0x03
#define KEY4_SHORT_PRESS 0x04  // KEY4短按码值 0x04
#define KEY1_DOUBLE_PRESS 0x51 // KEY1双击码值 0x51
#define KEY2_DOUBLE_PRESS 0x52 // KEY2双击码值 0x52
#define KEY3_DOUBLE_PRESS 0x53 // KEY3双击码值 0x53
#define KEY4_DOUBLE_PRESS 0x54 // KEY4双击码值 0x54
#define KEY1_LONG_PRESS 0x81   // KEY1长按码值 0x81
#define KEY2_LONG_PRESS 0x82   // KEY2长按码值 0x82
#define KEY3_LONG_PRESS 0x83   // KEY3长按码值 0x83
#define KEY4_LONG_PRESS 0x84   // KEY4长按码值 0x84

void KeyDrvInit(void);
uint8_t GetKeyVal(void);
void KeyDrvTest(void);

#endif
